<template>
  <el-header>
    <div class="left" v-if="showTitle" @click="toMain()">
      <img src="../assets/images/logo-min.png" />
      <p>文档管理系统</p>
    </div>
    <div class="right">
      <img src="../assets/images/person2.png" style="margin-right: 5px" />
      <p>{{ name }}</p>
      <img src="../assets/images/icon/logout.png" @click="logout" />
    </div>
  </el-header>
</template>

<script>
import tool from "@/utils/tool";

export default {
  name: "headerMain",
  data() {
    return {
      name: "",
      timer: 0,
      shrinkPacket: false,
      isOpenAdmin: false,
    };
  },
  props: {
    showTitle: {
      type: Boolean,
      default: true,
    },
  },
  components: {},
  mounted() {
    this.name = JSON.parse(this.$getCookie("userInfo")).userName;
    this.userId = JSON.parse(this.$getCookie("userInfo")).userId;
    this.$nextTick(() => {
      setTimeout(() => {
        let resourceIds = this.$getCookie("resourceIds").split(",");
        let manageIds = ["8", "9", "10", "11", "99"];
        this.isOpenAdmin =
          manageIds.filter(function (val) {
            return resourceIds.indexOf(val) > -1;
          }).length > 0
            ? true
            : false;
        this.totalNum = this.$store.state.totalNum;
      }, 500);
    });
    if (this.alarmStatus) {
      this.startRotate();
    } else {
      clearInterval(this.timer);
    }
  },
  computed: {
    totalNum: {
      get() {
        return this.$store.state.totalNum;
      },
      set(val) {
        this.$store.state.totalNum = val;
      },
    },
    alarmStatus() {
      return this.$store.state.alarmStatus;
    },
  },
  watch: {
    alarmStatus(v) {
      console.log(v);
      if (v) {
        this.startRotate();
      } else {
        clearInterval(this.timer);
      }
    },
  },
  methods: {
    toAdmin() {
      this.$router.push("/super");
    },
    toMain() {
      this.$router.push("/project");
    },
    toWarn() {
      this.$router.push("/warn");
    },
    startRotate() {
      clearInterval(this.timer);
      this.timer = setInterval(() => {
        this.shrinkPacket = !this.shrinkPacket;
      }, 270);
    },
    logout() {
      const token = tool.getCookie("X-Token");
      if (token) {
        this.$http.post("/ltcloud/user/logout").then((res) => {});
      }
      this.alarmStatus = false;
      window.localStorage.clear();
      this.$clearAllCookie();
      this.$disconnect();
      this.$store.commit("setAlarmStatus", false);
      localStorage.stop = true;
      const host =
        process.env.NODE_ENV == "development"
          ? process.env.VUE_APP_HOME_URL
          : process.env.VUE_APP_HOME_URL_PROD;

      window.close();
      window.open(host);
    },
  },
};
</script>

<style lang="less" scoped>
.fixed-box {
  position: fixed;
  top: 0px;
  width: 30px;
  height: 30px;
  img {
    width: 30px;
    height: 30px;
    margin-top: -4px;
  }
  .r1 {
    transform: translateY(6px);
  }
  .r2 {
    transform: translateY(8px);
  }
  .tips-box {
    display: inline-block;
    float: left;
    height: 18px;
    padding: 0 6px;
    line-height: 19px;
    font-size: 12px;
    color: #fff;
    border: 1px solid #fff;
    border-radius: 10px;
    white-space: nowrap;
    background-color: #f56c6c;
    margin-left: 15px;
  }
}
.seat-box {
  width: 30px;
  height: 30px;
  margin: 0px 10px;
}
.el-header {
  height: 78px;
  background: #fff;
  padding: 0 25px;
  display: flex;
  justify-content: space-between;
  position: fixed;
  width: 100%;
  z-index: 10;
  .left {
    display: flex;
    cursor: pointer;
    align-items: center;
    img {
      width: 37px;
      margin-right: 12px;
    }
    p {
      font-size: 18px;
      font-weight: 500;
      color: #161820;
      opacity: 1;
    }
  }
  .right {
    display: flex;
    align-items: center;
    img {
      width: 30px;
      margin-right: 20px;
      cursor: pointer;
      &:not(:first-of-type) {
        width: 30px;
        height: 30px;
      }
    }
    p {
      line-height: 27px;
      color: #445a5a;
      margin-right: 10px;
    }
  }
}
</style>
